package com.beyondsoft.mcms.common;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

public class FileUtil {

	public static ReturnInfo<Object> exportExcelFile(byte[] fileBytes, String fileName, HttpServletResponse response) {
		ReturnInfo<Object> returnInfo = new ReturnInfo<Object>();
		if (null == fileBytes || fileBytes.length <= 0) {
			returnInfo.setSuccess(false);
			return returnInfo;
		}
		OutputStream out = null;
		response.setContentType("application/octet-stream;charset=UTF-8");
		try {
			response.reset();
			fileName = URLEncoder.encode(fileName, "UTF-8");
			if (fileName.length() > 150)// IE 6.0
			{
				fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1");
			}
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ";size=" + fileBytes.length);
			out = response.getOutputStream();
			out.write(fileBytes);
			returnInfo.setSuccess(true);
		} catch (IOException e) {
			returnInfo.setSuccess(false);
			return returnInfo;
		} finally {
			if(null != out){
				try {
					out.close();
					out = null;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return returnInfo;
	}

}
